* Table A2a-g Balance Tests for individual level treatments from 2007-2013
* Sample used to study investments and agricultural outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

* using balanced_panel
keep if bal_panel3==1


local outcomevars "kval_w1_real cost_k_w1_real b110rk_w1_real b111rk_w1_real b112pk_w1_real total_mandays_k_w1 total_hired_mandays_k_w1 total_fam_mandays_k_w1 profit_k_w1_real  fraction_hy fraction_cashcrop plotsize_ha"

* identifying first year respondent was surveyed
sort id survey_year
by id: gen first_year=survey_year if [_n]==1 


foreach var of local outcomevars {
gen `var'_t0_temp = `var' if survey_year==first_year
bys id: egen `var'_t0 =min(`var'_t0_temp)   
drop `var'_t0_temp
}

foreach x of numlist 2006/2014 {
gen surveyed_`x'=0
replace surveyed_`x'=1 if surveyed==1 & survey_year==`x'
gen treat_year_`x'=0
replace treat_year_`x'=1 if treat_year==1 & survey_year==`x'
}

* this dataset gives you  baseline for the first year a respondent was surveyed
sort id wave
collapse (first) *_t0 villageno first_year (max) add_sample* surveyed_2* treat_year_2* , by(id)

save "../../../wi_datasets/Complete Panel/Dta/baseline-outcome-vars.dta", replace


** 2007

use  "../../../wi_datasets/Complete Panel/Dta/baseline-outcome-vars.dta", clear

** using actual assignments for 2007
** People from surveyed group in 2006 added to treatment, so condition on surveyed in 2006==1

merge 1:1 id using "../../../wi_datasets/Marketing/2007/dta/marketing_2007.dta", gen(_m2007)
keep if _m2007==3

** dropping the 322 folks added in wave 2

drop if add_sample_w2==1

** flyers were assigned only to people surveyed in the previous year so we condition on this
** videos were assigned to the new households

** combining both flyer and video treatments in one year, and including interaction effect
gen discount_video=discount*video

tsset, clear
set seed 7897

* only original villages
sort id 
local table=1

local col=1
foreach ovar of local outcomevars{
reg `ovar'_t0 hinduT muslimT groupT ppayT vframeT discount_video video if surveyedwave1==1, vce(bootstrap, cluster(villageno) reps(100) noisily)
estimates store cT`table'C`col++'

qui estout cT`table'C* using "../output/intended-bal-2007.csv", ///
	cells(b(star fmt(2)) se(par(`"="("' `")""'))  ) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (`inst`i'') prehead("`i'") ///
	mlabel(,depvar nonumbers) ///
	replace stats(N)
local col=`col'+1
}

tsset, clear
set seed 7897

sort id 
foreach ovar of local outcomevars{
local col = 1
xi: reg `ovar'_t0  hinduT muslimT groupT ppayT vframeT discount_video video if surveyedwave1==1, vce(bootstrap, cluster(villageno) reps(100) noisily)
est sto A
test  hinduT muslimT groupT ppayT vframeT discount_video 
estadd scalar pval = r(p)
esttab A using "../output/intended-jointsig-2007.csv", stats(N pval, fmt(0 2) labels("Obs""P-val"))  nonum nomtitles append
estimates clear
local col=`col'+1	
}

** 2008

use  "../../../wi_datasets/Complete Panel/Dta/baseline-outcome-vars.dta", clear


** using actual assignments for 2007

merge 1:1 id using "../../../wi_datasets/Marketing/2008/dta/marketing_2008_ready.dta", gen(_m2008)
keep if _m2008==3

** Dropping folks who weren't surveyed in 2006 or 2007 so we don't have baseline data for them
** We wouldn't have baseline data for these guys
** These are the folks who were added in wave 5, but were part of the listing and are included in the respondent list
drop if add_sample_w5==1

** wrong treatment assignments
drop if treat_year_2008==0

** generating treatment variables consistent with the earlier versions
replace flyer_exp=flyer_exp|flyer_hyv_exp
replace flyer_hyv=flyer_hyv|flyer_hyv_exp

** treatment assignment errors
foreach x of varlist discount rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv flyer_exp {
replace `x'=0 if treat_year_2008==0
}


tsset, clear
set seed 8798
sort id 
local table=1

local col=1
foreach ovar of local outcomevars{
reg `ovar'_t0  discount rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv flyer_exp , vce(bootstrap, cluster(villageno) reps(100) noisily)
estimates store cT`table'C`col++'

qui estout cT`table'C* using "../output/intended-bal-2008.csv", ///
	cells(b(star fmt(2)) se(par(`"="("' `")""'))  ) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (`inst`i'') prehead("`i'") ///
	mlabel(,depvar nonumbers) ///
	replace stats(N)
local col=`col'+1
}

tsset, clear
set seed 8798
sort id 
foreach ovar of local outcomevars{
local col = 1
xi: reg `ovar'_t0  discount rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv flyer_exp, vce(bootstrap, cluster(villageno) reps(100) noisily)
est sto A
test  discount rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv flyer_exp
estadd scalar pval = r(p)
esttab A using "../output/intended-jointsig-2008.csv", stats(N pval, fmt(0 2) labels("Obs""P-val"))  nonum nomtitles append
estimates clear
local col=`col'+1	
}

** Dropping the 322 folks who were added to boost sales
drop if add_sample_w2==1


** generating treatment variables consistent with the earlier versions
replace flyer_exp=flyer_exp|flyer_hyv_exp
replace flyer_hyv=flyer_hyv|flyer_hyv_exp

** treatment assignment errors
foreach x of varlist discount rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv flyer_exp {
replace `x'=0 if treat_year_2008==0
}

tsset, clear
set seed 8798
sort id 
local table=1

local col=1
foreach ovar of local outcomevars{
reg `ovar'_t0  discount rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv flyer_exp, vce(bootstrap, cluster(villageno) reps(100) noisily)
estimates store cT`table'C`col++'

qui estout cT`table'C* using "../output/intended-bal-2008-exclnew.csv", ///
	cells(b(star fmt(2)) se(par(`"="("' `")""'))  ) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (`inst`i'') prehead("`i'") ///
	mlabel(,depvar nonumbers) ///
	replace stats(N)
local col=`col'+1
}

sort id 
foreach ovar of local outcomevars{
local col = 1
xi: reg `ovar'_t0  discount rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv flyer_exp, vce(bootstrap, cluster(villageno) reps(100) noisily)
est sto A
test  discount rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv flyer_exp 
estadd scalar pval = r(p)
esttab A using "../output/intended-jointsig-2008-exclnew.csv", stats(N pval, fmt(0 2) labels("Obs""P-val"))  nonum nomtitles append
estimates clear
local col=`col'+1	
}

** 2009

use  "../../../wi_datasets/Complete Panel/Dta/baseline-outcome-vars.dta", clear
merge 1:1 id using "../../../wi_datasets/Marketing/2009/dta/mktg2009.dta", gen(_m2009)
keep if _m2009==3

gen fourbdmperc=bdmperc*disc4game

** Dropping folks who were not part of sample, added in later years
drop if add_sample_w5==1

** This drops the 322 folks that were added in earlier years 
drop if add_sample_w2==1

** Treatment assignment errors
foreach x of varlist disc4game bdmperc fourbdmperc {
replace `x'=0 if treat_year_2009==0
}

** wrong treatment assignments
drop if treat_year_2009==0

tsset, clear
set seed 455
sort id 
local table=1

local col=1
foreach ovar of local outcomevars{
reg `ovar'_t0  bdmperc disc4game fourbdmperc, vce(bootstrap, cluster(villageno) reps(100) noisily)
estimates store cT`table'C`col++'

qui estout cT`table'C* using "../output/intended-bal-2009.csv", ///
	cells(b(star fmt(2)) se(par(`"="("' `")""'))  ) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (`inst`i'') prehead("`i'") ///
	mlabel(,depvar nonumbers) ///
	replace stats(N)
local col=`col'+1
}

tsset, clear
set seed 455
sort id 
foreach ovar of local outcomevars{
local col = 1
xi: reg `ovar'_t0  bdmperc disc4game fourbdmperc, vce(bootstrap, cluster(villageno) reps(100) noisily)
est sto A
test  bdmperc disc4game fourbdmperc
estadd scalar pval = r(p)
esttab A using "../output/intended-jointsig-2009.csv", stats(N pval, fmt(0 2) labels("Obs""P-val"))  nonum nomtitles append
estimates clear
local col=`col'+1	
}

** 2010


use  "../../../wi_datasets/Complete Panel/Dta/baseline-outcome-vars.dta", clear

** using actual assignments for 2010
** there were blank ids here I had to drop them and save a new version
merge 1:1 id using "../../../wi_datasets/Marketing/2010/dta/marketing2010_ready-NS.dta", gen(_m2010)
keep if _m2010==3

* dropping the 322 added
drop if add_sample_w2==1

set seed 0101
sort id 
local table=1

local col=1
foreach ovar of local outcomevars{
reg `ovar'_t0  bdmperc disc4game fourbdmperc assigned_video_test assigned_drought assigned_subsidies_flyer assigned_risk_ws assigned_loan, vce(bootstrap, cluster(villageno) reps(100) noisily)
estimates store cT`table'C`col++'

qui estout cT`table'C* using "../output/intended-bal-2010.csv", ///
	cells(b(star fmt(2)) se(par(`"="("' `")""'))  ) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (`inst`i'') prehead("`i'") ///
	mlabel(,depvar nonumbers) ///
	replace stats(N)
local col=`col'+1
}

tsset, clear
set seed 455
sort id 
foreach ovar of local outcomevars{
local col = 1
xi: reg `ovar'_t0  bdmperc disc4game fourbdmperc assigned_video_test assigned_drought assigned_subsidies_flyer assigned_risk_ws assigned_loan, vce(bootstrap, cluster(villageno) reps(100) noisily)
est sto A
test  bdmperc disc4game fourbdmperc assigned_video_test assigned_drought assigned_subsidies_flyer assigned_risk_ws assigned_loan
estadd scalar pval = r(p)
esttab A using "../output/intended-jointsig-2010.csv", stats(N pval, fmt(0 2) labels("Obs""P-val"))  nonum nomtitles append
estimates clear
local col=`col'+1	
}

** 2011

use  "../../../wi_datasets/Complete Panel/Dta/baseline-outcome-vars.dta", clear

** using actual assignments for 2007
** there were blank ids here I had to drop them and save a new version
merge 1:1 id using "../../../wi_datasets/Marketing/2011/dta/marketing_2011.dta", gen(_m2011)
keep if _m2011==3

** dropping the 322 added in wave 2
drop if add_sample_w2==1

gen fourbdmperc=bdmperc*disc4game

tsset, clear
set seed 2812
sort id 
local table=1

local col=1
foreach ovar of local outcomevars{
reg `ovar'_t0  bdmperc disc4game fourbdmperc, vce(bootstrap, cluster(villageno) reps(100) noisily)
estimates store cT`table'C`col++'

qui estout cT`table'C* using "../output/intended-bal-2011.csv", ///
	cells(b(star fmt(2)) se(par(`"="("' `")""'))  ) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (`inst`i'') prehead("`i'") ///
	mlabel(,depvar nonumbers) ///
	replace stats(N)
local col=`col'+1
}

tsset, clear
set seed 2812
sort id 
foreach ovar of local outcomevars{
local col = 1
xi: reg `ovar'_t0  bdmperc disc4game fourbdmperc, vce(bootstrap, cluster(villageno) reps(100) noisily)
est sto A
test  bdmperc disc4game fourbdmperc
estadd scalar pval = r(p)
esttab A using "../output/intended-jointsig-2011.csv", stats(N pval, fmt(0 2) labels("Obs""P-val"))  nonum nomtitles append
estimates clear
local col=`col'+1	
}

** 2012

use  "../../../wi_datasets/Complete Panel/Dta/baseline-outcome-vars.dta", clear

merge 1:1 id using "../../../wi_datasets/Marketing/2012/dta/marketing_2012.dta", gen(_m2012)
keep if _m2012==3

** dropping the 322 added in wave 2
drop if add_sample_w2==1

gen fourbdmperc=bdmperc*disc4game

tsset, clear
set seed 956
sort id 
local table=1

local col=1
foreach ovar of local outcomevars{
reg `ovar'_t0  bdmperc disc4game fourbdmperc, vce(bootstrap, cluster(villageno) reps(100) noisily)
estimates store cT`table'C`col++'

qui estout cT`table'C* using "../output/intended-bal-2012.csv", ///
	cells(b(star fmt(2)) se(par(`"="("' `")""'))  ) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (`inst`i'') prehead("`i'") ///
	mlabel(,depvar nonumbers) ///
	replace stats(N)
local col=`col'+1
}

tsset, clear
set seed 956
sort id 
foreach ovar of local outcomevars{
local col = 1
xi: reg `ovar'_t0  bdmperc disc4game fourbdmperc, vce(bootstrap, cluster(villageno) reps(100) noisily)
est sto A
test  bdmperc disc4game fourbdmperc
estadd scalar pval = r(p)
esttab A using "../output/intended-jointsig-2012.csv", stats(N pval, fmt(0 2) labels("Obs""P-val"))  nonum nomtitles append
estimates clear
local col=`col'+1	
}


** 2013

use  "../../../wi_datasets/Complete Panel/Dta/baseline-outcome-vars.dta", clear

merge 1:1 id using "../../../wi_datasets/Marketing/2013/dta/marketing_2013.dta", gen(_m2013)
keep if _m2013==3

drop if add_sample_w2==1


gen fourbdmperc=bdmperc*disc4game

tsset, clear
set seed 2015
sort id 
local table=1

local col=1
foreach ovar of local outcomevars{
reg `ovar'_t0  bdmperc disc4game fourbdmperc, vce(bootstrap, cluster(villageno) reps(100) noisily)
estimates store cT`table'C`col++'

qui estout cT`table'C* using "../output/intended-bal-2013.csv", ///
	cells(b(star fmt(2)) se(par(`"="("' `")""'))  ) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (`inst`i'') prehead("`i'") ///
	mlabel(,depvar nonumbers) ///
	replace stats(N)
local col=`col'+1
}

tsset, clear
set seed 2015
sort id 
foreach ovar of local outcomevars{
local col = 1
xi: reg `ovar'_t0  bdmperc disc4game fourbdmperc, vce(bootstrap, cluster(villageno) reps(100) noisily)
est sto A
test  bdmperc disc4game fourbdmperc
estadd scalar pval = r(p)
esttab A using "../output/intended-jointsig-2013.csv", stats(N pval, fmt(0 2) labels("Obs""P-val"))  nonum nomtitles append
estimates clear
local col=`col'+1	
}
